/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.upload;

import queenb.web.util.ResourcesProvider;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;


import queenb.web.util.Util;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import queenb.web.bean.FileDescription;
import queenb.web.controller.QueenbController;
import queenb.web.store.M650Store;
import queenb.web.util.ComboboxUtil;
import queenb.web.util.ContextUtil;
import queenb.web.util.SessionProvider;

/**
 *
 * @author ems
 */
public class UploadController extends QueenbController {

    private static Logger logger = Logger.getLogger(UploadController.class);
    private int pn100;
    private int pm650 = 0;
    private String isavata;
    private int num;
    private String time;
    private String type = "jpg,png,bmp,gif";
    private String table;
    private String pagereload = "";
    private String mv651;
    private String pvFc150;
    private String mv659 = null;
    private String active = "";
    private int fk100;
    private int fs200;
    private String emp;
    private List<SelectItem> listF000 = new ArrayList<SelectItem>();
    private int ff000;
    private int fv050DO;
    private String fv051DO;
    private int tip;// Tung : phân biệt tai liêu cua khach hang or tour
    //private String pagemyprofile;

    public UploadController() {
        super();
        Object statusObject = getRequest().getParameter("status");
        try {
            if (statusObject != null && statusObject.equals("T")) {
                tip = 0;
            } else {
                tip = 1;
            }
        } catch (Exception e) {
        }
        Object pvPC150 = getRequest().getParameter("pc150");
        Object pnPN100 = getRequest().getParameter("pn100");
        Object pnPM650 = getRequest().getParameter("pm650");
        Object pvtype = getRequest().getParameter("type");
        Object pvtable = getRequest().getParameter("table");
        Object pvpage = getRequest().getParameter("page");
        Object objEmp = getRequest().getParameter("emp");
        Object objfv050DO = getRequest().getParameter("fv050");
        Object objfv051DO = getRequest().getParameter("fv051");
        if (objEmp != null) {
            emp = objEmp.toString();
        } else {
            emp = getLogin().getUserName();
        }
        Object objFV050 = getRequest().getParameter("fs200");
        if (objFV050 != null) {
            this.fs200 = Integer.parseInt(objFV050.toString());
        }
        if (pvtype != null) {
            type = "*";
        }
        if (pvtable != null) {
            table = pvtable.toString();
        }
        if (pvpage != null && pvpage.toString().contains("employee.jsp")) {
            table = "N100";
        }
        if (pvpage != null && pvpage.toString().contains("supplier.jsp")) {
            table = "K100";
        }
        if (pvpage != null && pvpage.toString().contains("customer.jsp")) {
            table = "N100";
        }
        if (pvpage != null && pvpage.toString().contains("cuscompany.jsp")) {
            table = "K100";
        }
//        if (pvpage != null && pvpage.toString().contains("project.jsp")) {
//            table = "C150";
//        }
        if (pvpage != null && pvpage.toString().contains("myprofile.jsp")) {
            table = "M650";
        }
        if (pvpage != null && pvpage.toString().contains("partner.jsp")) {
            table = "K100";
        }
        if (pvpage != null && pvpage.toString().contains("documentmanager.jsp")) {
            table = "N100";
        }if (pvpage != null && pvpage.toString().contains("productTour.jsp")) {
            table = "S450";
        }



        try {
            this.pagereload = getRequest().getParameter("page");
            if (this.pagereload == null) {
                this.pagereload = ContextUtil.getRequestParameter("page");
            }
            if (pagereload.equals("cuscompany.jsp")) {
                table = "K100";
            }
            //this.pagereload = pages.toString();

        } catch (Exception ex) {
        }
        try {
            if (pnPN100 != null) {
                System.out.println("---------- NGUYEN TUNG------" + pnPN100);
                pn100 = Util.parseInt(pnPN100);

            }
        } catch (Exception e) {
        }

        if (pnPN100 != null && !pnPN100.toString().isEmpty() && pnPM650 != null) {
            pn100 = Integer.parseInt(pnPN100.toString());
            if (pnPM650.toString().contains("none")) {
                isavata = "N";
                pm650 = 0;
                num = 10;
            } else {
                isavata = "A";
                pm650 = Integer.parseInt(pnPM650.toString());
                num = 1;
            }
            SessionProvider.setSessionMapValue("iduser", pn100 + "--" + pm650 + "--" + isavata);
            SessionProvider.setSessionMapValue("mv651", "");

        }
        if (pvPC150 != null) {
            mv659 = pvPC150.toString();
            SessionProvider.setSessionMapValue("mv659", mv659);

        }
//Duy
        if (objfv050DO != null) {
            try {
                fv050DO = Integer.parseInt(objfv050DO.toString());
            } catch (Exception e) {
                System.out.println("D:- Error from setobjfv050DO: " + e.getMessage());
                fv050DO = 0;
            }
            SessionProvider.setSessionMapValue("fv050DO", fv050DO);

        }

        if (objfv051DO != null) {
            try {
                fv051DO = objfv051DO.toString();
            } catch (Exception e) {
                System.out.println("D:- Error from setobjfv051DO: " + e.getMessage());
                fv051DO = "";
            }
            SessionProvider.setSessionMapValue("fv051DO", fv051DO);

        }


        System.out.println("---------- upload-----table-" + table + "---mv659---" + mv659 + "---mv651---" + mv651);
    }

    public void save(ActionEvent ac) {
        try {
            System.out.println("saveeeeee" + SessionProvider.getSessionMapValue("iduser") + mv651 + "---------------------Folder" + ff000);
           
                SessionProvider.setSessionMapValue("mv651", mv651);
    
            SessionProvider.setSessionMapValue("ff000", ff000);
        } catch (Exception e) {
            System.out.println("error---save--:" + e.getMessage());
        }

    }

    public void uploadDocListener(UploadEvent ue) throws IOException {
        try {
            // System.out.println("==++++++++++++++++++++++ start "+ this.pagereload);
            uploadAttachment(ue, FileDescription.DOC_KIND_OF_FILE);

        } catch (Exception e) {
            System.out.println("error---------:" + e.getMessage());
        }


    }

    private void uploadAttachment(UploadEvent ue, int typeUpload) {
        
        List<UploadItem> uploadItem = ue.getUploadItems();
        if (SessionProvider.getSessionMapValue("mv659") != null) {
            mv659 = SessionProvider.getSessionMapValue("mv659").toString();

            if (SessionProvider.getSessionMapValue("mv651") != null) {
                mv651 = SessionProvider.getSessionMapValue("mv651").toString();

            }
            if (SessionProvider.getSessionMapValue("ff000") != null) {
                ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000").toString());

            }
        } else if (SessionProvider.getSessionMapValue("iduser") != null) {
//            if (mv651 == null || mv651.equals("")) {
//                SessionProvider.setSessionMapValue("mv651", "");
//            }
//            if (ff000 != 0) {
//                 SessionProvider.setSessionMapValue("ff000", ff000);
//            }
            String[] id = SessionProvider.getSessionMapValue("iduser").toString().split("--");
            pn100 = Integer.parseInt(id[0]);
            pm650 = Integer.parseInt(id[1]);
            isavata = id[2];
            if (SessionProvider.getSessionMapValue("mv651") != null && !SessionProvider.getSessionMapValue("mv651").equals("")) {
                mv651 = SessionProvider.getSessionMapValue("mv651").toString();
            } else {
                SessionProvider.setSessionMapValue("mv651", "");
                if (SessionProvider.getSessionMapValue("mv651") != null) {
                    mv651 = SessionProvider.getSessionMapValue("mv651").toString();
                }
            }
            if (SessionProvider.getSessionMapValue("ff000").toString() != null) {
                ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000").toString());
            } else {
                SessionProvider.setSessionMapValue("ff000", "1");
                if (SessionProvider.getSessionMapValue("ff000") != null) {
                    ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000"));
                }
            }
            

            logger.debug("==++++++++++++++++++++++ size" + uploadItem.size() + "-->" + pn100);
        }
        Object ofv050 = SessionProvider.getSessionMapValue("fv050DO");
        if (ofv050 != null) {
            try {
                this.fv050DO = Integer.parseInt(ofv050.toString());
            } catch (Exception e) {
                this.fv050DO = 0;
            }

        }
        Object ofv051 = SessionProvider.getSessionMapValue("fv051DO");
        if (ofv051 != null) {
            this.fv051DO = ofv051.toString();
        }


        M650Store attachmentStore = new M650Store();
        for (int i = 0; i < uploadItem.size(); i++) {
            try {
                UploadItem uploadItem1 = uploadItem.get(i);
                FileDescription fileDesciption = Util.saveFile(uploadItem1);
                if (SessionProvider.getSessionMapValue("mv651") != null && !SessionProvider.getSessionMapValue("mv651").equals("")) {
                    mv651 = SessionProvider.getSessionMapValue("mv651").toString();
                } else {
                    SessionProvider.setSessionMapValue("mv651", "");
                    if (SessionProvider.getSessionMapValue("mv651") != null) {
                        mv651 = SessionProvider.getSessionMapValue("mv651").toString();
                        
                    }
                }
                if(SessionProvider.getSessionMapValue("ff000")!=null){
                    ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000").toString());
                }else{
                    ff000 = 4;// upload vào Tour
                }
                
                int reVal = attachmentStore.updateM650(
                        pm650, loginSession.getLogin().getModule(), loginSession.getLogin().getCompanyNo(), 0, 0, mv659, 0,
                        mv651, fileDesciption.getFileName(), fileDesciption.getNewName(),
                        0,
                        null, getTable(), fv050DO, pn100, fv051DO, getIsavata(), ff000,
                        loginSession.getLogin().getUserName());

                showGlobalReport(true, getResourceString("datasavesuccessful"));
               
                 SessionProvider.setSessionMapValue("getFileImage", fileDesciption.getNewName());
                 System.out.println("-----------------"+ SessionProvider.getSessionMapValue("getFileImage"));
                SessionProvider.removeSessionMapValue("mv659");
               // SessionProvider.removeSessionMapValue("mv651");
                setFf000(1);

            } catch (Exception ex) {
                SessionProvider.removeSessionMapValue("mv659");
                SessionProvider.removeSessionMapValue("mv651");
                System.out.println("ERROR:COULD NOT ATTACH FILE:CODE:" + ex.getMessage());
                showGlobalReport(true, getResourceString("importFail"));
            }
            System.out.println("success.jsp" + ResourcesProvider.getDownloadFolder("abc.jsp"));
        }



    }

    /**
     * @return the pn100
     */
    public int getPn100() {
        return pn100;
    }

    /**
     * @param pn100 the pn100 to set
     */
    public void setPn100(int pn100) {
        this.pn100 = pn100;
    }

    /**
     * @return the pm650
     */
    public int getPm650() {
        return pm650;
    }

    /**
     * @param pm650 the pm650 to set
     */
    public void setPm650(int pm650) {
        this.pm650 = pm650;
    }

    /**
     * @return the isavata
     */
    public String getIsavata() {
        return isavata;
    }

    /**
     * @param isavata the isavata to set
     */
    public void setIsavata(String isavata) {
        this.isavata = isavata;
    }

    /**
     * @return the num
     */
    public int getNum() {
        return num;
    }

    /**
     * @param num the num to set
     */
    public void setNum(int num) {
        this.num = num;
    }

    /**
     * @return the time
     */
    public String getTime() {
        long id = new Date().getTime();
        if (pagereload.equals("myprofile")) {
            time = "myprofile.jsp?fn100M=" + pn100;
        } else if (pagereload.equals("customer.jsp")) {
            time = "customer.jsp?tab=itemcontent" + pn100 + "&flag=" + id + "&#" + pn100;
        } else if (pagereload.equals("cuscompany.jsp")) {
             time = "cuscompany.jsp?tab=itemcontent" + pn100 + "&flag=" + id + "&#" + pn100;
        } else if (pagereload.equals("employee.jsp")) {
            time = "employee.jsp?tab=tab4" + pn100 + "&flag=" + id + "&#" + pn100;
        }  else if (pagereload.equals("employeenew.jsp")) {
            time = "employeenew.jsp?tab=itemcontent" + pn100 + "&flag=" + id + "&#" + pn100;
        } else if (pagereload.equals("supplier.jsp")) {
            time = "supplier.jsp?tab=tab3" + pn100 + "&flag=" + id + "&#" + pn100;
        } else if (pagereload.equals("project.jsp")) {
            time = "project.jsp?&flag=" + id + "&#" + pn100 + "&emp=" + emp;
        } else if (pagereload.equals("partner.jsp")) {
            time = "partner.jsp?tab=tab3" + pn100 + "&flag=" + id + "&#" + pn100;
        } else if (pagereload.equals("documentmanager.jsp")) {
            time = "documentmanager.jsp";
        } else if (pagereload.equals("financialAccount.jsp")) {
            time = "financialAccount.jsp";
        } else if (pagereload.equals("debt.jsp")) {
            time = "debt.jsp";
        } else if (pagereload.equals("customer4tour.jsp")) {
            time = "customer4tour.jsp";
        }  else if (pagereload.equals("productTour.jsp")) {
            time = "productTour.jsp";
        } else {
            time = "documentTour.jsp?tab=" + pn100;
        }


        return time;
    }

    /**
     * @param time the time to set
     */
    public void setTime(String time) {
        this.time = time;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @return the pagereload
     */
    public String getPagereload() {
        return pagereload;
    }

    /**
     * @param pagereload the pagereload to set
     */
    public void setPagereload(String pagereload) {
        this.pagereload = pagereload;
    }

    /**
     * @return the mv651
     */
    public String getMv651() {
        
        return mv651;
    }

    /**
     * @param mv651 the mv651 to set
     */
    public void setMv651(String mv651) {
        this.mv651 = mv651;
    }
//    /**
//     * @return the pagemyprofile
//     */
//    public String getPagemyprofile() {
//        return pagemyprofile;
//    }
//
//    /**
//     * @param pagemyprofile the pagemyprofile to set
//     */
//    public void setPagemyprofile(String pagemyprofile) {
//        this.pagemyprofile = pagemyprofile;
//    }

    /**
     * @return the table
     */
    public String getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(String table) {
        this.table = table;
    }

    /**
     * @return the active
     */
    public String getActive() {
        return active;
    }

    /**
     * @param active the active to set
     */
    public void setActive(String active) {
        this.active = active;
    }

    /**
     * @return the fk100
     */
    public int getFk100() {
        return fk100;
    }

    /**
     * @param fk100 the fk100 to set
     */
    public void setFk100(int fk100) {
        this.fk100 = fk100;
    }

    /**
     * @return the fv050
     */
    public int getFs200() {
        return fs200;
    }

    /**
     * @param fv050 the fv050 to set
     */
    public void setFs200(int fs200) {
        this.fs200 = fs200;
    }

    /**
     * @return the pvFc150
     */
    public String getPvFc150() {
        return pvFc150;
    }

    /**
     * @param pvFc150 the pvFc150 to set
     */
    public void setPvFc150(String pvFc150) {
        this.pvFc150 = pvFc150;
    }

    /**
     * @return the emp
     */
    public String getEmp() {
        return emp;
    }

    /**
     * @param emp the emp to set
     */
    public void setEmp(String emp) {
        this.emp = emp;
    }

    /**
     * @return the listF000
     */
    public List<SelectItem> getListF000() {
        ComboboxUtil combo = new ComboboxUtil();
        listF000 = combo.getlistCombF000();
        return listF000;
    }

    /**
     * @param listF000 the listF000 to set
     */
    public void setListF000(List<SelectItem> listF000) {
        this.listF000 = listF000;
    }

    /**
     * @return the fk000
     */
    public int getFf000() {
        return ff000;
    }

    /**
     * @param fk000 the fk000 to set
     */
    public void setFf000(int ff000) {
        this.ff000 = ff000;
    }

    /**
     * @return the fv050DO
     */
    public int getFv050DO() {
        return fv050DO;
    }

    /**
     * @param fv050DO the fv050DO to set
     */
    public void setFv050DO(int fv050DO) {
        this.fv050DO = fv050DO;
    }

    /**
     * @return the fv051DO
     */
    public String getFv051DO() {
        return fv051DO;
    }

    /**
     * @param fv051DO the fv051DO to set
     */
    public void setFv051DO(String fv051DO) {
        this.fv051DO = fv051DO;
    }

    /**
     * @return the tip
     */
    public int getTip() {
        return tip;
    }

    /**
     * @param tip the tip to set
     */
    public void setTip(int tip) {
        this.tip = tip;
    }
    
    
    public void uploadDocListenerTour(UploadEvent ue) throws IOException {
        try {
            // System.out.println("==++++++++++++++++++++++ start "+ this.pagereload);
            uploadAttachmentTour(ue, FileDescription.DOC_KIND_OF_FILE);

        } catch (Exception e) {
            System.out.println("error---------:" + e.getMessage());
        }


    }

    private void uploadAttachmentTour(UploadEvent ue, int typeUpload) {
        
        List<UploadItem> uploadItem = ue.getUploadItems();
        if (SessionProvider.getSessionMapValue("mv659") != null) {
            mv659 = SessionProvider.getSessionMapValue("mv659").toString();

            if (SessionProvider.getSessionMapValue("mv651") != null) {
                mv651 = SessionProvider.getSessionMapValue("mv651").toString();

            }
            if (SessionProvider.getSessionMapValue("ff000") != null) {
                ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000").toString());

            }
        } else if (SessionProvider.getSessionMapValue("iduser") != null) {
//            if (mv651 == null || mv651.equals("")) {
//                SessionProvider.setSessionMapValue("mv651", "");
//            }
//            if (ff000 != 0) {
//                 SessionProvider.setSessionMapValue("ff000", ff000);
//            }
            String[] id = SessionProvider.getSessionMapValue("iduser").toString().split("--");
            pn100 = Integer.parseInt(id[0]);
            pm650 = Integer.parseInt(id[1]);
            isavata = id[2];
            if (SessionProvider.getSessionMapValue("mv651") != null && !SessionProvider.getSessionMapValue("mv651").equals("")) {
                mv651 = SessionProvider.getSessionMapValue("mv651").toString();
            } else {
                SessionProvider.setSessionMapValue("mv651", "");
                if (SessionProvider.getSessionMapValue("mv651") != null) {
                    mv651 = SessionProvider.getSessionMapValue("mv651").toString();
                }
            }
            if (SessionProvider.getSessionMapValue("ff000").toString() != null) {
                ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000").toString());
            } else {
                SessionProvider.setSessionMapValue("ff000", "1");
                if (SessionProvider.getSessionMapValue("ff000") != null) {
                    ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000"));
                }
            }
            

            logger.debug("==++++++++++++++++++++++ size" + uploadItem.size() + "-->" + pn100);
        }
        Object ofv050 = SessionProvider.getSessionMapValue("fv050DO");
        if (ofv050 != null) {
            try {
                this.fv050DO = Integer.parseInt(ofv050.toString());
            } catch (Exception e) {
                this.fv050DO = 0;
            }

        }
        Object ofv051 = SessionProvider.getSessionMapValue("fv051DO");
        if (ofv051 != null) {
            this.fv051DO = ofv051.toString();
        }


        M650Store attachmentStore = new M650Store();
        for (int i = 0; i < uploadItem.size(); i++) {
            try {
                UploadItem uploadItem1 = uploadItem.get(i);
                FileDescription fileDesciption = Util.saveFileTour(uploadItem1);
                if (SessionProvider.getSessionMapValue("mv651") != null && !SessionProvider.getSessionMapValue("mv651").equals("")) {
                    mv651 = SessionProvider.getSessionMapValue("mv651").toString();
                } else {
                    SessionProvider.setSessionMapValue("mv651", "");
                    if (SessionProvider.getSessionMapValue("mv651") != null) {
                        mv651 = SessionProvider.getSessionMapValue("mv651").toString();
                        
                    }
                }
                if(SessionProvider.getSessionMapValue("ff000")!=null){
                    ff000 = Util.parseInt(SessionProvider.getSessionMapValue("ff000").toString());
                }else{
                    ff000 = 4;// upload vào Tour
                }
                
                int reVal = attachmentStore.updateM650(
                        pm650, loginSession.getLogin().getModule(), loginSession.getLogin().getCompanyNo(), 0, 0, mv659, 0,
                        mv651, fileDesciption.getFileName(), fileDesciption.getNewName(),
                        0,
                        null, getTable(), fv050DO, pn100, fv051DO, getIsavata(), ff000,
                        loginSession.getLogin().getUserName());

                showGlobalReport(true, getResourceString("datasavesuccessful"));
               
                 SessionProvider.setSessionMapValue("getFileImage", fileDesciption.getNewName());
                SessionProvider.removeSessionMapValue("mv659");
               // SessionProvider.removeSessionMapValue("mv651");
                setFf000(1);

            } catch (Exception ex) {
                SessionProvider.removeSessionMapValue("mv659");
                SessionProvider.removeSessionMapValue("mv651");
                System.out.println("ERROR:COULD NOT ATTACH FILE:CODE:" + ex.getMessage());
                showGlobalReport(true, getResourceString("importFail"));
            }
            System.out.println("success.jsp" + ResourcesProvider.getDownloadFolder("abc.jsp"));
        }



    }
}
